package com.changyinBus.web.controller.system;

import com.changyinBus.common.annotation.Log;
import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.common.core.domain.entity.SysNavi;
import com.changyinBus.common.enums.BusinessType;
import com.changyinBus.common.utils.IDGenerator;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.system.service.ISysNaviService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 导航管理
 */
@RestController
@RequestMapping("/system/navi")
public class SysNaviContorller extends BaseController {
    @Resource
    private ISysNaviService naviService;

    /**
     * 获取导航列表
     */
    @PreAuthorize("@ss.hasPermi('system:navi:list')")
    @GetMapping("/list")
    public AjaxResult list(SysNavi navi) {
        List<SysNavi> depts = naviService.selectNaviList(navi);
        return success(depts);
    }

    /**
     * 根据导航编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:navi:query')")
    @GetMapping(value = "/{naviId}")
    public AjaxResult getInfo(@PathVariable Long naviId) {
        return success(naviService.selectNaviById(naviId));
    }

    /**
     * 新增导航
     */
    @PreAuthorize("@ss.hasPermi('system:navi:add')")
    @Log(title = "导航管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNavi navi) {
        navi.setNavId(IDGenerator.getID());
        navi.setCreateBy(getUsername());
        return success(naviService.insertNavi(navi));
    }

    /**
     * 修改导航
     */
    @PreAuthorize("@ss.hasPermi('system:navi:edit')")
    @Log(title = "导航管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNavi navi) {
        if (navi.getParentId() == navi.getNavId()) {
            return error("修改导航'" + navi.getNavName() + "'失败，上级分类不能是自己");
        }
        navi.setUpdateBy(getUsername());
        return toAjax(naviService.updateNavi(navi));
    }

    /**
     * 查询导航列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:navi:list')")
    @PostMapping("/list/exclude")
    public AjaxResult excludeChild(@RequestBody SysNavi navi) {
        SysNavi param = new SysNavi();
        param.setTenantId(navi.getTenantId());
        List<SysNavi> depts = naviService.selectNaviList(param);
        depts.removeIf(d -> d.getNavId() == navi.getNavId() || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), navi.getNavId() + ""));
        return success(depts);
    }

    /**
     * 删除导航
     */
    @PreAuthorize("@ss.hasPermi('system:navi:remove')")
    @Log(title = "导航管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{naviId}")
    public AjaxResult remove(@PathVariable Long naviId) {
        if (naviService.hasChildByNaviId(naviId) > 0) {
            return warn("存在下级分类,不允许删除");
        }
        return toAjax(naviService.deleteNaviById(naviId));
    }

    /**
     * 获取分类树列表
     */
    @GetMapping("/naviTree")
    public AjaxResult naviTree(SysNavi navi) {
        return success(naviService.selectNaviTreeList(navi));
    }
}
